package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.think.teaching.mapper.CollegeMapper;
import com.think.teaching.model.College;
import com.think.teaching.model.Major;
import com.think.teaching.service.CollegeService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 院系 服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class CollegeServiceImpl extends BaseServiceImpl<CollegeMapper, College> implements CollegeService {

	@Override
	public College AddNewCollege(College college) {
		if (this.save(college)) {
			return college;
		}
		return null;
	}

	@Override
	public College UpdateCollege(College college) {
		if (this.updateById(college)) {
			return college;
		}
		return null;
	}

	@Override
	public College GetCollegeInfo(Integer id) {
		QueryWrapper<College> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public List<Major> ListCollegeMajor(Boolean withoutDelete) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	
	public College SetCollegeState(Integer id, Integer state) {
		UpdateWrapper<College> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

	public IPage<College> ListCollegeWithSchool(Integer count, Integer page, Integer school,
			Boolean withoutBan) {
		QueryWrapper<College> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		if (school>0) {
			queryWrapper.eq("school", school);
		}
		IPage<College> ipage = new Page<>(page, count);
		return this.page(ipage,queryWrapper);
	}

	@Override
	public List<College> SearchCollegeWithKey(String key, Boolean withoutBan) {
		QueryWrapper<College> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1).like("college_name", key);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		queryWrapper.last("limit 0 , 10");
		return list(queryWrapper);
	}
	
	public List<Major> getCollegeMajors(Integer id) {
		return baseMapper.getCollegeMajorsById(id);
	}

}
